package com.community.web.master.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.community.model.entity.Orders;
import com.community.web.master.domain.dto.PageOrdersDto;
import com.community.web.master.domain.vo.OrderWithDetailVo;
import com.community.web.master.domain.vo.PageResult;
import com.community.web.master.exception.DataException;
import com.community.web.master.mapper.OrdersMapper;
import com.community.web.master.service.IOrdersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 李晶
 * @since 2024-07-02
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements IOrdersService {
    @Autowired
    private OrdersMapper ordersMapper;

    /**
     * 订单分页查询
     * @param pageOrdersDto
     * @return
     */
    @Override
    public PageResult pageQuery(PageOrdersDto pageOrdersDto) {
        IPage<Orders> page = new Page<Orders>(pageOrdersDto.getPage(),pageOrdersDto.getPageSize());
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<Orders>();
        //条件判断
        if(pageOrdersDto.getOrderNo()!=null){
            if (!pageOrdersDto.getOrderNo().equals("")) queryWrapper.like("order_no",pageOrdersDto.getOrderNo());
        }
        if(pageOrdersDto.getOrderState()!=null){
           queryWrapper.eq("order_state",pageOrdersDto.getOrderState());
        }

        if(pageOrdersDto.getBeginTime() != null){
            queryWrapper.ge("create_time", pageOrdersDto.getBeginTime());
        }
        if(pageOrdersDto.getEndTime() != null){
            queryWrapper.le("create_time", pageOrdersDto.getEndTime());
        }
        ordersMapper.selectPage(page,queryWrapper);
        return new PageResult(page.getTotal(),page.getRecords());
    }

    /**
     * 根据订单号查看订单详情
     * @param orderNo
     * @return
     */
    @Override
    public List<OrderWithDetailVo> getOrderDetailByOrderNo(String orderNo) {
        Orders orders = ordersMapper.selectOne(new QueryWrapper<Orders>().eq("order_no", orderNo));
        if (orders==null) throw new DataException("订单不存在");
        //查两个表Goods:商品名称,图片  orderDetail:商品数量,单价,小计
        List<OrderWithDetailVo> result = ordersMapper.getOrderDetailByOrderNo(orderNo);
        return result;
    }

    /**
     * 批量发货
     * @param ids
     */

    @Override
    public void deliveryBatch(List<Integer> ids) {
        List<Orders> orders = ordersMapper.selectBatchIds(ids);
        if (orders == null | orders.isEmpty()) throw new DataException("数据有误");
        for (Orders order : orders) {
            if(order.getOrderState()!=2) throw new DataException("有订单不是已付款状态,无法发货");
        }
        UpdateWrapper<Orders> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id",ids);
        updateWrapper.set("order_state",3);
        ordersMapper.update(null,updateWrapper);

    }
}
